package com.atguigu.web;

import com.atguigu.pojo.Cart;
import com.atguigu.pojo.Order;
import com.atguigu.pojo.OrderItem;
import com.atguigu.pojo.User;
import com.atguigu.service.OrderService;
import com.atguigu.service.impl.OrderServiceImpl;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class OrderServlet extends BaseServlet {
    private OrderService orderService = new OrderServiceImpl();

    /**
     * 创建表单
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void createOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取购物车内容
        Cart cart = (Cart) req.getSession().getAttribute("cart");
        //获取用户的id
        User user = (User) req.getSession().getAttribute("user");
        if (user == null) {
            req.getRequestDispatcher("/pages/user/login.jsp").forward(req, resp);
            return;
        }
        Integer id = user.getId();
        String orderId = orderService.createOrder(cart, id);
//        req.setAttribute("orderId",orderId);
        //请求转发到pages/cart/checkout.jsp
//        req.getRequestDispatcher("/pages/cart/checkout.jsp").forward(req,resp);
        //改为重定向  防止重复提交
        req.getSession().setAttribute("orderId",orderId);
        resp.sendRedirect(req.getContextPath() + "/pages/cart/checkout.jsp");
    }

    /**
     * 获取所有的该用户的订单单号
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void showAllOrders(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //先获取所有的表单
        List<Order> orders = orderService.showAllOrders();
        //将所有的得到的表单放进域中
        req.setAttribute("orders", orders);
        //请求转发到order_manager.jsp
        req.getRequestDispatcher("/pages/manager/order_manager.jsp").forward(req, resp);
    }

    /**
     * 管理员发货
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void sendOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取当前订单单号
        String orderId = req.getParameter("orderId");
        //发货
        int i = orderService.sendOrder(orderId);
        //重新返回本页面
        String referer = req.getHeader("Referer");
        resp.sendRedirect(referer);
    }

    /**
     * 用户查看自己的订单
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void showMyOders(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取用户的id
        User user = (User) req.getSession().getAttribute("user");
        if (user == null) {
            req.getRequestDispatcher("/pages/user/login.jsp").forward(req, resp);
            return;
        }
        Integer id = user.getId();
        //先获取所有的表单
        List<Order> orders = orderService.showMyOrder(id);
        //将所有的得到的表单放进域中
        req.setAttribute("orders", orders);
        //请求转发到order_manager.jsp
        req.getRequestDispatcher("/pages/order/order.jsp").forward(req, resp);
    }

    /**
     * 展示自己的所有的订单
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void showOdersDetail(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取订单单号
        String orderId = req.getParameter("orderId");
        User user = (User) req.getSession().getAttribute("user");
        //调用方法
        List<OrderItem> orderItems = orderService.showOrderDetail(orderId);
        List<Order> orders = orderService.showMyOrder(user.getId());
        req.setAttribute("orders", orders);
        //保存到域中
        req.setAttribute("orderItems", orderItems);
        //请求转到
        req.getRequestDispatcher("/pages/manager/order_managers.jsp").forward(req, resp);
    }

    /**
     * 确认收货
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void receiverOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取用户的订单id
        String orderId = req.getParameter("orderId");
//        调用service中的方法
        int i = orderService.receiverOrder(orderId);
        //重新返回本页面
        String referer = req.getHeader("Referer");
        resp.sendRedirect(referer);
    }

}
